package org.dromara.neutrinoproxy.server.controller;


import org.apache.commons.lang3.RandomStringUtils;
import org.dromara.neutrinoproxy.server.base.proxy.ProxyConfig;
import org.dromara.neutrinoproxy.server.controller.res.proxy.FileUploadRes;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.UploadedFile;

import java.io.File;
import java.io.IOException;

@Mapping("/file")
@Controller
public class FileController {

    @Inject
    private ProxyConfig proxyConfig;

    @Post
    @Mapping("/upload")
    public FileUploadRes upload(@Param("file") UploadedFile file) throws IOException {
        String filePath = proxyConfig.getServer().getFilePath();
        String filename = RandomStringUtils.random(32, true, true) + file.getName();
        File saveFile = new File(filePath + File.separator + filename);
        file.transferTo(saveFile);
        return new FileUploadRes(saveFile.getPath());
    }

}
